package controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import jdk.internal.util.xml.impl.ReaderUTF8;
import model.Cart;
import model.Goods;
import model.Order;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IGoodsService;
import service.IShopService;
import service.IUserService;
import service.impl.ShopService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private IShopService iShopService;


    @RequestMapping("list")
    public ModelAndView list(Goods goods, HttpServletRequest request){
        HttpSession session = request.getSession();
        //根据id从session中获取管理员对象
        User m = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", m);
        //商品一级分类
        List<Goods> parent = iShopService.selectParentId(null);
        mv.addObject("parent", parent);
        //商品二级分类
        List<Goods> sort = iShopService.selectSortId(null);
        mv.addObject("sort", sort);
        //全部商品分类
        List<Goods> list = iShopService.selectGoods(null);
        mv.addObject("list", list);
        mv.setViewName("shop/index");
        return  mv;
    }

    @RequestMapping("listBySort")
    public ModelAndView listBySort(Goods goods, HttpServletRequest request){
        return  iShopService.listBySort(goods, request);
    }
    @RequestMapping("listByParent")
    public ModelAndView listByParent(Goods goods, HttpServletRequest request){
        return iShopService.listByParent(goods, request);
    }

    @RequestMapping("detailPage")
    public ModelAndView detailPage(Goods goods, HttpServletRequest request){
        HttpSession session = request.getSession();
        User m = (User) session.getAttribute("user");
        ModelAndView mv= new ModelAndView();
        Goods g = iShopService.selectGoods(goods).get(0);
        mv.addObject("good", g);
        mv.addObject("user", m);
        mv.setViewName("shop/detail");
        return  mv;
    }

    @RequestMapping("cartPage")
    public ModelAndView cartPage(Cart cart ,HttpServletRequest request){
        return  iShopService.selectCart(cart, request);
    }
    @RequestMapping("addCart")
    @ResponseBody
    public String addCart(Cart cart, Goods goods){

        return iShopService.insertCart(cart, goods);
    }
    @RequestMapping("orderPage")
    public ModelAndView orderPage(Goods goods, HttpServletRequest request, String orderNo){
        return  iShopService.order(goods, request, orderNo);
    }
    @RequestMapping(value = "order" , produces = "text/html;charset=utf-8")
    @ResponseBody
    public String order(Order order, Goods goods){
        return  iShopService.order(order, goods);
    }
    @RequestMapping("payPage")
    public ModelAndView payPage(String orderNo , Integer uId){
        System.out.println("!!!!!!!!!!"+orderNo);
        ModelAndView mv = new ModelAndView();
        mv.addObject("orderNo", orderNo);
        mv.addObject("uId", uId);
        mv.setViewName("shop/place_pay");
        return  mv;
    }
    @RequestMapping("pay")
    @ResponseBody
    public String pay(Integer id, String pass, String orderNo){
        return  iShopService.pay(id, pass, orderNo);
    }

    @RequestMapping("deleteCart")
    @ResponseBody
    public String deleteCart(Integer id){
        System.out.println("id="+id);
        return iShopService.deleteCart(id);
    }


}
